﻿using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Badger.Vhdl;
using Weazel.Badger.Vhdl.Types;
using Weazel.Badger.Vhdl.Statements;
using Weazel.Badger.Vhdl.Expressions;

namespace Weazel.Willows.InterfaceGeneration
{
	public class StatusRegisterHardwareToSoftware
		: MemoryMappedItem
	{
		private List<HardwareToSoftwareQueue> queues;
		public List<HardwareToSoftwareQueue> Queues
		{
			get { return queues; }
		}

		public int AddressesNeeded
		{
			get { return queues.Count / 32 + 1; }
		}

		public StatusRegisterHardwareToSoftware(MemoryMap memoryMap)
			: base(memoryMap)
		{
			this.queues = new List<HardwareToSoftwareQueue>();
		}

		public void Add(HardwareToSoftwareQueue connection)
		{
			queues.Add(connection);
		}

		public override string ToString()
		{
			return "Hardware to software Queue status register";
		}

		public override void SetupReadProcessCase(UserLogicEntity entity)
		{			
		}

		public override void SetupWriteProcessCase(UserLogicEntity entity)
		{
			// a status register cannot be written by the bus
			for (int i = 0; i < AddressesNeeded; i++)
			{
				Expression choiceExpression =
					GetChoiceExpression(
						entity.WriteProcessOnClockStatementCollection,
						GetRelativeAddressAsPositionalString(RelativeBaseAddress + i)
					);

				CaseStatementChoice choice =
					new CaseStatementChoice(choiceExpression);

				choice.Statements.Add(new Comment("Cannot write status register"));
				choice.Statements.Add(new BlackBoxStatement("null;"));
				entity.WriteProcessCaseStatement.AddChoice(choice);
			}
		}
	}
}
